﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;

public partial class MyInvoices : System.Web.UI.Page
{
    List<_Invoice> list = new List<_Invoice>();

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.IsAuthenticated)
        {
            string n = "";
            SqlCommand sCmd = new SqlCommand("SELECT name FROM USERS WHERE id=@id", conn);
            sCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
            conn.Open();
            SqlDataReader dr = sCmd.ExecuteReader();
            while (dr.Read())
            {
                string name = Convert.ToString(dr[0].ToString());
                n = name;
            }
            dr.Close();
            conn.Close();

            WelcomeBackMessage.Text = "Welcome back, " + n + "!";

            AuthenticatedHeaderPanel.Visible = true;
            AuthenticatedMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = true;

            if (!IsPostBack)
            {
                SetData();
            }
        }
        else
        {
            AuthenticatedHeaderPanel.Visible = false;
            AuthenticatedMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = false;
        }

    }

    private string Sort
    {
        get
        {
            return String.Concat(this.SortExpression, (this.SortDirection == SortDirection.Ascending) ? " DESC" : " ASC");
        }
    }
    private SortDirection SortDirection
    {
        get
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = SortDirection.Descending;
            }

            return (SortDirection)ViewState["SortDirection"];
        }
        set { ViewState["SortDirection"] = value; }
    }
    private string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] == null)
            {
                ViewState["SortExpression"] = "ID";
            }

            return ViewState["SortExpression"].ToString();
        }
        set { ViewState["SortExpression"] = value; }
    }

    protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
    {
        if (this.SortExpression.Equals(e.SortExpression))
        {
            this.SortDirection = (this.SortDirection == SortDirection.Descending)
                  ? SortDirection.Ascending
                  : SortDirection.Descending;
        }
        else
        {
            this.SortDirection = SortDirection.Descending;
        }

        this.SortExpression = e.SortExpression;
        this.SetData();
    }

    private void SetData()
    {
        list = _Invoice.Select_My_Invoices("","",User.Identity.Name);
        if (list.Count == 0)
        {
            message.Visible = true;
            bt_delete_all_my_invoices.Visible = false;
        }
        else
        {
            message.Visible = false;
            ListView1.DataSource = list;
            ListView1.DataBind();
            bt_delete_all_my_invoices.Visible = true;
        }
    }

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Invoice")
        {
            var id_document = e.Item.FindControl("doc_IDLabel") as Label;
            Session["Doc_ID"] = id_document.Text;
            Response.Redirect("ViewInvoice.aspx");
        }

        if (e.CommandName == "Edit")
        {
            //Iegūstam id_issued_user un document_id ID un padodam _User un _invoive klasei lai atlasītu datus priekš EditDocument.aspx
            var id_user = e.Item.FindControl("id_issued_userLabel") as Label;
            var id_document = e.Item.FindControl("doc_IDLabel") as Label;
            Session["User_ID"] = id_user.Text;
            Session["Doc_ID"] = id_document.Text;
            Response.Redirect("EditDocment.aspx");
        }

        //if (e.CommandName == "Delete")
        //{
        //    var id_document = e.Item.FindControl("doc_IDLabel") as Label;
        //    string document_ID = id_document.Text;
        //    _Article DeleteArticle = _Article.Delete_all_articles(document_ID);
        //    _Invoice DeleteInvoice = _Invoice.Delete(document_ID);
        //    Response.Redirect("MyInvoices.aspx");
        //}

        if (e.CommandName == "Share")
        {
            var id_document = e.Item.FindControl("doc_IDLabel") as Label;
            Session["Doc_ID"] = id_document.Text;

            var id_issued_user = e.Item.FindControl("id_issued_userLabel") as Label;
            Session["userID"] = id_issued_user.Text;

            var doc_num = e.Item.FindControl("document_numLabel") as Label;
            Session["Doc_Num"] = doc_num.Text;

            Response.Redirect("Share.aspx");
        }
    }

    // Slēpjam nevajadzīgās kolonnas
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        e.Item.FindControl("idL").Visible = false;
        e.Item.FindControl("docID").Visible = false;
        e.Item.FindControl("doc_num").Visible = false;
    }

    protected void bt_delete_all_my_invoices_Click(object sender, EventArgs e)
    {
        List<string> id_document = new List<string>();
        foreach (ListViewItem item in ListView1.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("check_delete_doc");
            if (cb.Checked)
            {
                Label id_doc = (Label)item.FindControl("doc_IDLabel");
                string doc_id = id_doc.Text;
                id_document.Add(doc_id);
            }
        }
        if (id_document.Count == 0)
        {
            HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Select Invoice!')</SCRIPT>");
        }
        else
        {
            foreach (string document in id_document)
            {
                //_Article.Delete_all_articles(document);
            }

            foreach (string document in id_document)
            {
                //_Invoice.Delete(document);
            }
        }

        Response.Redirect("MyInvoices.aspx");
    }

    public void OnConfirm(object sender, EventArgs e)
    {
        string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "Jā")
        {
            Label id_document = null;
            foreach (ListViewDataItem i in ListView1.Items)
            {
                id_document = i.FindControl("doc_IDLabel") as Label;
            }
            string document_ID = id_document.Text;
            //_Article.Delete_all_articles(document_ID);
            ////_Invoice.Delete(document_ID);
            Response.Redirect("MyInvoices.aspx");
        }
        else if (confirmValue == "Atcelt")
        {
            Response.Redirect("MyInvoices.aspx");
        }
    }
}